package com.ruoyi.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.Audit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.Red;
import com.ruoyi.service.RedService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 红包Controller
 *
 * @author ruoyi
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/system/red")
public class RedController extends BaseController
{
    @Autowired
    private RedService redService;

    /**
     * 查询红包列表
     */
    @PostMapping("/list")
    public AjaxResult page(@RequestBody Page<Red> page, @RequestBody Red red){
        System.out.println(page.getCurrent() + "我是page对象"+red);
        //查询数据
        redService.page(page,new LambdaQueryWrapper<Red>()
                //内容
                .like(StringUtils.isNotBlank(red.getRedName()),Red::getRedName,red.getRedName())
                //逻辑删除
                .eq(Red::getDeleteFlag,Boolean.FALSE)
        );
        //设置总行数
        page.setTotal(redService.count(new LambdaQueryWrapper<Red>()
                //内容
                .like(StringUtils.isNotBlank(red.getRedName()),Red::getRedName,red.getRedName())
                //逻辑删除
                .eq(Red::getDeleteFlag,Boolean.FALSE)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    /**
     * 获取红包详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(redService.getById(id));
    }

    /**
     * 新增红包
     */
    @Log(title = "红包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Red red)
    {
        red.setDeleteFlag(Boolean.FALSE);
        return toAjax(redService.save(red));
    }

    /**
     * 修改红包
     */
    @Log(title = "红包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Red red)
    {
        return toAjax(redService.updateById(red));
    }

    /**
     * 删除红包
     */
    @Log(title = "红包", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(redService.removeById(id));
    }
}
